<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/12/22
 * Time: 13:59
 */
namespace app\web\controller;

use app\web\service\AliService;
use app\web\service\LoginService;
use think\facade\Log;

class Login{
    /**
     * @todo 获取授权连接
     * @author ZJF
     */
    public function getAuthorUrl(){
        return json_ok(array('url'=>AliService::getAuthorUrl()),10019);
    }

    /**
     * @todo 重定向到前端页面
     * @return \think\response\Json|\think\response\Redirect
     */
    public function redirect(){
        $code = request()->param('code/s','');//授权码
        if(empty($code)){
            return json_ok([],1,"无效的授权码");
        }
        return redirect("https://quickprint.xingyebao.com/#/?code={$code}");
    }

    /**
     * @todo 授权登录
     * @author ZJF
     */
    public function Login(){
        $code = request()->param('code/s','');//授权码
        if(empty($code)){
            return json_ok([],1,"无效的授权码");
        }
//        Log::write("code:{$code}",'INFO');
//        print_data($code);
        $login_service = new LoginService();
        $res = $login_service->login($code);
        if($res['code']!=0){
            return json_ok($res['data'],$res['code'],$res['message']);
        }
        return json_ok($res['data']);
    }
}